package org.example.book.conf;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitmqConfig {
    // 正常队列
    public static final String NORMAL_QUEUE_NAME = "normalQueue";

    public static final String NORMAL_EXCHANGE_NAME = "normalExchange";

    public static final String NORMAL_ROUTING_KEY = "normalRoutingKey";

    // 死信队列
    public static final String DEAD_QUEUE_NAME = "deadQueue";

    public static final String DEAD_EXCHANGE_NAME = "deadExchange";

    public static final String DEAD_ROUTING_KEY = "deadRoutingKey";

    @Bean
    public Queue normalQueue() {
        return new Queue(NORMAL_QUEUE_NAME);
    }

    @Bean
    public DirectExchange normalExchange() {
        return new DirectExchange(NORMAL_EXCHANGE_NAME);
    }

    /**
     * 定义绑定信息
     * @param normalQueue
     * @param normalExchange
     * @return
     */
    @Bean
    public Binding normalBinding(Queue normalQueue, DirectExchange normalExchange) {
        return BindingBuilder.bind(normalQueue).to(normalExchange).with(NORMAL_ROUTING_KEY);
    }

    @Bean
    public Queue deadQueue() {
        return new Queue(DEAD_QUEUE_NAME);
    }

    @Bean
    public DirectExchange deadExchange() {
        return new DirectExchange(DEAD_EXCHANGE_NAME);
    }

    /**
     * 定义绑定信息
     * @param deadQueue
     * @param deadExchange
     * @return
     */
    @Bean
    public Binding deadBinding(Queue deadQueue, DirectExchange deadExchange) {
        return BindingBuilder.bind(deadQueue).to(deadExchange).with(DEAD_ROUTING_KEY);
    }
}
